clear

*Load Data
use "./data_geo.dta", clear

*****************************************************************************
*** Setup: see Kashyap & Stein (2000) and/or Den Haan, Sumner, Yamashiro (2002) for details ***

*Drop non-states 
drop if rssd9200 == "0"
keep if  rssd9210>0 & rssd9210< 57

*Drop Delaware and/or South Dakota (see Jayaratne & Strahan 1998 JLE)
*drop if rssd9210 == 10
*drop if rssd9210 == 46

*Sample period
keep if year_q < tq(1995q1)

*Positive Assets
keep if rcfd2170 > 0

*Commercial Banks Only
keep if rssd9048 == 200 

*Insured
*keep if rssd9424 == 1 | rssd9424 == 2 | rssd9424 == 6
drop if rssd9424 == 0

*Merger
drop if MERGE_CD != .

*Set panel data
xtset rssd9001 year_q

*Fix Total Loans pre-1984 (insert before calcuating loans2 above)
gen loans2 = rcfd1400
replace loans2 = rcfd1400+rcfd2165 if year_q < tq(1984q1)

*Put loans and loan components in real terms
gen c_p_i = cpi/100
gen r_loans = loans2/c_p_i 
gen r_ci = rcfd1600/c_p_i
gen r_rlest = rcfd1410/c_p_i
gen r_con = rcfd1975/c_p_i
gen r_ag = rcfd1590/c_p_i

*Log difference 
gen loans = ln(r_loans) - ln(L.r_loans)
gen ci = ln(r_ci) - ln(L.r_ci) 
gen re = ln(r_rlest) - ln(L.r_rlest) 
gen con = ln(r_con) - ln(L.r_con)
gen ag = ln(r_ag) - ln(L.r_ag) 

*Drop outliers
sort year_q
by year_q: egen sdv = sd(loans)
drop if loans >= 5*sdv
drop if loans <= -5*sdv

*Loan Component Outliers
gen ci_share = rcfd1600/loans2
*drop if ci_share < 0.05
gen re_share = rcfd1410/loans2
*drop if re_share < 0.05
gen con_share = rcfd1975/loans2
*drop if con_share < 0.05
gen ag_share = rcfd1590/loans2
*drop if ag_share < 0.05

*Four Consecutive Quarters of Loan Growth
*ssc install tsspell
tsspell, c(loans > 0)
sort rssd9001
by rssd9001: egen maxrun = max(_seq)
drop if maxrun < 4

*Percentiles
sort year_q
by year_q: egen p95 = pctile(rcfd2170), p(95)
by year_q: egen p99 = pctile(rcfd2170), p(99)

*Generate Quarter/Year/timetrend Series
sort rssd9001 year_q
gen Quarter = quarter(date2)
gen Year = year(date2) 
egen time = group(year_q)

*Choose to keep (or drop) year deregulation was implemented 
replace intra = 1 if intra == .
replace inter =1 if inter == .

* Generate Interaction Terms
gen rr_intra = rr*intra
gen rr_intra1 = L1.rr*intra
gen rr_intra2 = L2.rr*intra
gen rr_intra3 = L3.rr*intra
gen rr_intra4 = L4.rr*intra

gen rr_inter = rr*inter
gen rr_inter1 = L1.rr*inter
gen rr_inter2 = L2.rr*inter
gen rr_inter3 = L3.rr*inter
gen rr_inter4 = L4.rr*inter

gen ffr_intra = ffr_end*intra
gen ffr_intra1 = L1.ffr_end*intra
gen ffr_intra2 = L2.ffr_end*intra
gen ffr_intra3 = L3.ffr_end*intra
gen ffr_intra4 = L4.ffr_end*intra

gen ffr_inter = ffr_end*inter
gen ffr_inter1 = L1.ffr_end*inter
gen ffr_inter2 = L2.ffr_end*inter
gen ffr_inter3 = L3.ffr_end*inter
gen ffr_inter4 = L4.ffr_end*inter


*******************************************************************************
*** Table 2 ***

*** Panel A ***
xtreg loans L(1/4).loans L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx L(0/4).rr intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.Quarter i.Year, fe vce(cluster rssd9210) nonest
lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
lincom rr_intra + rr_intra1 + rr_intra2 + rr_intra3 + rr_intra4
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4
*ssc install outreg2  /*Full results for Table 5*/
*outreg2 using "baseline", excel replace label 

xtreg loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.year_q, fe vce(cluster rssd9210) nonest
lincom rr_intra + rr_intra1 + rr_intra2 + rr_intra3 + rr_intra4
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

xtreg loans L(1/4).loans L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx L(0/4).rr inter rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.Quarter i.Year, fe vce(cluster rssd9210) nonest
lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

xtreg loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg inter rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.year_q, fe vce(cluster rssd9210) nonest
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

xtreg loans L(1/4).loans L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx L(0/4).rr intra rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 i.Quarter i.Year, fe vce(cluster rssd9210) nonest
lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
lincom rr_intra + rr_intra1 + rr_intra2 + rr_intra3 + rr_intra4

xtreg loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg intra rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 i.year_q, fe vce(cluster rssd9210) nonest
lincom rr_intra + rr_intra1 + rr_intra2 + rr_intra3 + rr_intra4

******************************************************************************
*** Generate Avg Loan Rate ***

gen int_income = riad4010
replace int_income = int_income[_n+1]/2 if Quarter == 1 & Year < 1983
replace int_income = (int_income[_n-1]+int_income[_n+1])/2 if Quarter == 3 & Year < 1983

gen int_income2 = int_income - L.int_income if Quarter > 1 
replace int_income2 = int_income if int_income2 == .

gen avg_rate2 = int_income2/loans2
gen avg_rate = avg_rate2*4

*replace avg_rate = . if avg_rate > 4 | avg_rate < 0  /*Note: in the Jan_JMP draft I stipulate IF avg_rate < 4 & avg_rate > 0 for the first 2 regressions below; not sure about 1983-94 ones*/

*** Panel B ***
xtreg avg_rate L(1/4).avg_rate L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx L(0/4).rr intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.Quarter i.Year if avg_rate <4 & avg_rate > 0, fe vce(cluster rssd9210) nonest
lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

xtreg avg_rate L(1/4).avg_rate L(0/4).pi_pchg L(0/4).hpi_chg intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.year_q if avg_rate <4 & avg_rate > 0, fe vce(cluster year_q) nonest
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

xtreg avg_rate L(1/4).avg_rate L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx L(0/4).rr intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.Quarter i.Year if year_q > tq(1983q3) & avg_rate <4 & avg_rate > 0, fe vce(cluster rssd9210) nonest 
lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

xtreg avg_rate L(1/4).avg_rate L(0/4).pi_pchg L(0/4).hpi_chg intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.year_q if year_q > tq(1983q3) & avg_rate <4 & avg_rate > 0, fe vce(cluster year_q) nonest 
lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4


*** The below 2 regressions use all of 1983 instead of beginning in 1983q4 ***

*xtreg avg_rate L(1/4).avg_rate L(0/4).pi_pchg L(0/4).hpi_chg intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.year_q if Year > 1982 & avg_rate <4 & avg_rate > 0, fe vce(cluster year_q) nonest 
*lincom rr_intra + rr_intra1 + rr_intra2 + rr_intra3 + rr_intra4
*lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

*xtreg avg_rate L(1/4).avg_rate L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx L(0/4).rr intra inter rr_intra rr_intra1 rr_intra2 rr_intra3 rr_intra4 rr_inter rr_inter1 rr_inter2 rr_inter3 rr_inter4 i.Quarter i.Year if Year > 1982 & avg_rate <4 & avg_rate > 0, fe vce(cluster rssd9210) nonest 
*lincom rr + L1.rr + L2.rr + L3.rr + L4.rr
*lincom rr_intra + rr_intra1 + rr_intra2 + rr_intra3 + rr_intra4
*lincom rr_inter + rr_inter1 + rr_inter2 + rr_inter3 + rr_inter4

******************************************************************************
*** Panel c ***

gen rr_sum = rr + L1.rr + L2.rr + L3.rr + L4.rr

gen rrsum_intra = rr_sum*intra
gen rrsum_inter = rr_sum*inter

xtreg loans L(1/4).loans L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx rr_sum intra inter rrsum_intra rrsum_inter i.Quarter i.Year, fe vce(cluster rssd9210) nonest
lincom rr_sum
lincom rrsum_inter

xtreg loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg intra inter rrsum_intra rrsum_inter i.year_q, fe vce(cluster rssd9210) nonest
lincom rrsum_inter

xtreg loans L(1/4).loans L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx rr_sum intra inter rrsum_intra rrsum_inter i.Quarter i.Year  if rr_sum >=0, fe vce(cluster rssd9210) nonest
lincom rr_sum
lincom rrsum_inter

xtreg loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg intra inter rrsum_intra rrsum_inter i.year_q if rr_sum >=0, fe vce(cluster rssd9210) nonest
lincom rrsum_inter

xtreg loans L(1/4).loans L(0/4).gdp_chg L(0/4).pce_chg L(0/4).pi_pchg L(0/4).hpi_chg L(0/4).vwretx rr_sum intra inter rrsum_intra rrsum_inter i.Quarter i.Year if rr_sum <=0, fe vce(cluster rssd9210) nonest
lincom rr_sum
lincom rrsum_inter

xtreg loans L(1/4).loans L(0/4).pi_pchg L(0/4).hpi_chg intra inter rrsum_intra rrsum_inter i.year_q if rr_sum <=0, fe vce(cluster rssd9210) nonest
lincom rrsum_inter

********************************************************************************